import 'dart:convert';

import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/services.dart';

class ApiDocSummary {
  String id;
  String title;
  String url;
  String? author;

  ApiDocSummary({
    required this.id,
    required this.title,
    required this.url,
    this.author,
  });

  factory ApiDocSummary.fromJson(Map<String, dynamic> json) {
    return ApiDocSummary(
      id: json['id'],
      title: json['title'],
      url: json['url'],
      author: json['author'],
    );
  }

  String get docUrl => "http://doc.meiyibao.com/web/#/2?page_id=$id";

  String? get authorName => null;
}

Iterable<ApiDocSummary>? _apiSummaryList;

void initApiSummary() async {
  try {
    final jsonString =
        await rootBundle.loadString("assets/config/apis_summary.json");
    final m = jsonDecode(jsonString);
    _apiSummaryList = (m as List).map((e) => ApiDocSummary.fromJson(e));
  } catch (e) {
    print(e);
  }
}

ApiDocSummary? queryApiSummaryBy(String? url) {
  return _apiSummaryList
      ?.firstWhereOrNull((element) => element.url.endsWith(url!));
}
